<?php

if ( !function_exists('tr_install') ) :
function tr_install($blog_title, $admin_name, $admin_pass, $user_email, $prefix, $charset, $meta='') {
	global $tr_rewrite;

	tr_check_mysql_version();
	
	$schema = ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
	$guessurl = preg_replace('|/install/.*|i', '', $schema . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);

	tr_install_defaults($blog_title, $admin_name,$admin_pass, $user_email, $guessurl, $prefix, $charset);

	return array('url' => $guessurl, 'admin_name' => $admin_name, 'admin_pass' => $admin_pass);
}
endif;

function trans_sql_utf8($charset,$query){
    //if($charset=='utf-8')
    //    $query = iconv("gb2312", "UTF-8//IGNORE" , $query);
    return $query;
}

if ( !function_exists('tr_install_defaults') ) :
function tr_install_defaults($blog_title, $user_name,$user_pass, $user_email, $guessurl, $prefix, $charset) {
    $db_charstring = "";
    $db_charset = "";
    $db_collate = "";
    
    $user_pass = md5($user_pass);
    
    if($charset=="utf-8"){
        $db_charset = "utf8";
        $db_collate = "utf8_general_ci";
        $db_charstring = " CHARACTER SET $db_charset COLLATE $db_collate";
    }
    elseif($charset=="gb2312"){
        $db_charset = "gb2312";
        $db_collate = "gb2312_chinese_ci";
        $db_charstring = " CHARACTER SET $db_charset COLLATE $db_collate";
    }
    elseif($charset=="gbk"){
        $db_charset = "gbk";
        $db_collate = "gbk_chinese_ci";
        $db_charstring = " CHARACTER SET $db_charset COLLATE $db_collate";
    }
    else{
        $db_charstring = "";
    }
    
	global $trdb;
	
	$trdb->query(trans_sql_utf8($charset,"CREATE TABLE ".$prefix."users (
  ID bigint(20) unsigned NOT NULL auto_increment,
  user_login varchar(60)$db_charstring NOT NULL default '',
  user_pass varchar(64)$db_charstring NOT NULL default '',
  user_email varchar(100)$db_charstring NOT NULL default '',
  user_fullname varchar(50)$db_charstring NOT NULL default '',
  user_nickname varchar(50)$db_charstring NOT NULL default '',
  user_s_email varchar(100)$db_charstring NOT NULL default '',
  user_msn varchar(100)$db_charstring NOT NULL default '',
  user_blog varchar(100)$db_charstring NOT NULL default '',
  user_ip varchar(15)$db_charstring NOT NULL default '',
  user_registered datetime NOT NULL default '0000-00-00 00:00:00',
  user_level int(2) unsigned NOT NULL default '0',
  user_idmode varchar(20)$db_charstring NOT NULL default '',
  user_activation_key varchar(60)$db_charstring NOT NULL default '',
  user_status int(11) NOT NULL default '0',
  user_description longtext$db_charstring NOT NULL default '',
  user_addon1 varchar(50)$db_charstring NOT NULL default '',
  user_addon2 varchar(50)$db_charstring NOT NULL default '',
  user_addon3 varchar(50)$db_charstring NOT NULL default '',
  PRIMARY KEY  (ID),
  UNIQUE KEY user_login (user_login)
);"));
	
	$trdb->query(trans_sql_utf8($charset,"CREATE TABLE ".$prefix."options (
  option_id bigint(20) NOT NULL auto_increment,
  blog_id int(11) NOT NULL default '0',
  option_name varchar(64)$db_charstring NOT NULL default '',
  option_can_override enum('Y','N') NOT NULL default 'Y',
  option_type int(11) NOT NULL default '1',
  option_value longtext$db_charstring NOT NULL,
  option_width int(11) NOT NULL default '20',
  option_height int(11) NOT NULL default '8',
  option_description tinytext$db_charstring NOT NULL,
  option_admin_level int(11) NOT NULL default '1',
  autoload enum('yes','no') NOT NULL default 'yes',
  PRIMARY KEY  (option_id,blog_id,option_name),
  KEY option_name (option_name)
);"));
	
	$trdb->query(trans_sql_utf8($charset,"CREATE TABLE ".$prefix."posts (
  ID bigint(20) unsigned NOT NULL auto_increment,
  post_author int(11) NOT NULL default '0',
  post_date datetime NOT NULL default '0000-00-00 00:00:00',
  post_date_gmt datetime NOT NULL default '0000-00-00 00:00:00',
  post_content longtext$db_charstring NOT NULL,
  post_title text$db_charstring NOT NULL,
  post_tag int(6) NOT NULL default '0',
  post_excerpt text$db_charstring NOT NULL,
  post_status enum('publish','draft','private','static','object','problem') NOT NULL default 'publish',
  comment_status enum('open','closed','registered_only') NOT NULL default 'open',
  post_password varchar(20)$db_charstring NOT NULL default '',
  post_name varchar(200)$db_charstring NOT NULL default '',
  post_modified datetime NOT NULL default '0000-00-00 00:00:00',
  post_modified_gmt datetime NOT NULL default '0000-00-00 00:00:00',
  post_content_filtered text$db_charstring NOT NULL,
  post_parent int(11) NOT NULL default '0',
  guid varchar(255)$db_charstring NOT NULL default '',
  menu_order int(11) NOT NULL default '0',
  post_url text$db_charstring NOT NULL,
  favor_count int(10) NOT NULL default '0',
  post_favor enum('yes','no') NOT NULL default 'no',
  post_favor_date datetime NOT NULL default '0000-00-00 00:00:00',
  post_addon1 varchar(50)$db_charstring NOT NULL default '',
  post_addon2 varchar(50)$db_charstring NOT NULL default '',
  post_addon3 varchar(50)$db_charstring NOT NULL default '',
  PRIMARY KEY  (ID)
);"));
	
	$trdb->query(trans_sql_utf8($charset,"CREATE TABLE ".$prefix."tags (
  tag_ID bigint(20) NOT NULL auto_increment,
  tag_name varchar(55)$db_charstring NOT NULL default '',
  tag_nicename varchar(200)$db_charstring NOT NULL default '',
  tag_description longtext$db_charstring NOT NULL,
  tag_parent int(10) NOT NULL default '0',
  PRIMARY KEY  (tag_ID),
  KEY tag_nicename (tag_nicename)
);"));
	
	$trdb->query(trans_sql_utf8($charset,"CREATE TABLE ".$prefix."post2tag (
  rel_id bigint(20) NOT NULL auto_increment,
  post_id bigint(20) NOT NULL default '0',
  tag_id bigint(20) NOT NULL default '0',
  PRIMARY KEY  (rel_id),
  KEY post_id (post_id,tag_id)
);"));
	
	$trdb->query(trans_sql_utf8($charset,"CREATE TABLE ".$prefix."post2user (
  rel_id bigint(20) NOT NULL auto_increment,
  post_id bigint(20) NOT NULL default '0',
  user_id bigint(20) NOT NULL default '0',
  PRIMARY KEY  (rel_id),
  KEY post_id (post_id,user_id)
);"));
	
	$trdb->query(trans_sql_utf8($charset,"CREATE TABLE ".$prefix."postaddurl (
  rel_id bigint(20) NOT NULL auto_increment,
  post_id bigint(20) NOT NULL default '0',
  post_url text$db_charstring NOT NULL,
  PRIMARY KEY  (rel_id)
);"));
	
	$trdb->query(trans_sql_utf8($charset,"CREATE TABLE ".$prefix."postproblem (
  rel_id bigint(20) NOT NULL auto_increment,
  post_id bigint(20) NOT NULL default '0',
  user_id bigint(20) NOT NULL default '0',
  problem_id int(4) NOT NULL default '0',
  submit_date datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (rel_id),
  KEY post_id (post_id,user_id,problem_id)
);"));
	
	$trdb->query(trans_sql_utf8($charset,"CREATE TABLE ".$prefix."comments (
  comment_ID bigint(20) unsigned NOT NULL auto_increment,
  comment_post_id bigint(20) NOT NULL default '0',
  comment_user_id bigint(20) NOT NULL default '0',
  comment_user_ip varchar(100)$db_charstring NOT NULL default '',
  comment_date datetime NOT NULL default '0000-00-00 00:00:00',
  comment_date_gmt datetime NOT NULL default '0000-00-00 00:00:00',
  comment_content text$db_charstring NOT NULL,
  comment_karma int(11) NOT NULL default '0',
  comment_approved enum('0','1','spam') NOT NULL default '1',
  comment_type varchar(20)$db_charstring NOT NULL default '',
  comment_parent int(11) NOT NULL default '0',
  request_email enum('no','yes') NOT NULL default 'no',
  PRIMARY KEY  (comment_ID),
  KEY comment_approved (comment_approved),
  KEY comment_post_id (comment_post_id)
);"));
	
	$trdb->query(trans_sql_utf8($charset,"ALTER TABLE ".$prefix."tags ADD tag_long varchar(20)$db_charstring NOT NULL default '', ADD tag_lati varchar(20)$db_charstring NOT NULL default '', ADD zoom_in int(5) NOT NULL default '0', ADD photo_url text NOT NULL default '';"));
	
	
	$trdb->query(trans_sql_utf8($charset,"INSERT INTO `".$prefix."options` ( `option_id` , `blog_id` , `option_name` , `option_can_override` , `option_type` , `option_value` , `option_width` , `option_height` , `option_description` , `option_admin_level` , `autoload` ) 
VALUES (
NULL , '0', 'stylesheet', 'Y', '1', 'default', '20', '8', '样式名称，默认default', '1', 'yes'
), (
NULL , '0', 'sitetitle', 'Y', '1', '$blog_title', '20', '8', '站点名称', '1', 'yes'
), (
NULL , '0', 'admin_email', 'Y', '1', '$user_email', '20', '8', '管理员邮件地址', '1', 'yes'
), (
NULL , '0', 'page_post_count', 'Y', '1', '10', '20', '8', '首页显示的post数', '1', 'yes'
), (
NULL , '0', 'favor_count_to_home', 'Y', '1', '5', '20', '8', '每到这个数游记被提升到首页', '1', 'yes'
), (
NULL , '0', 'queue_post_count', 'Y', '1', '15', '20', '8', '队列页面显示的post数', '1', 'yes'
), (
'', '0', 'userpage_post_count', 'Y', '1', '15', '20', '8', '用户收藏夹中每页显示的post数', '1', 'yes'
), (
NULL , '0', 'favor_count_level2', 'Y', '1', '3', '20', '8', '收藏框的颜色级别2', '1', 'yes'
), (
NULL , '0', 'favor_count_level3', 'Y', '1', '5', '20', '8', '收藏框的颜色级别3', '1', 'yes'
), (
NULL , '0', 'userlist_count', 'Y', '1', '30', '20', '8', '用户列表每页显示的用户数目', '1', 'yes'
), (
NULL , '0', 'top_news', 'Y', '1', '<P><A href=http://www.ericfish.com/hologon/>Hologon Digg System</A> 已经安装成功！<br/>点击<A href=./?user=admin&profile>admin个人设置</A>，修改管理员初始密码。<br/>点击<A href=./?optionadmin>站点设置</A>，修改网站设置。</P>', '20', '8', '最新消息', '1', 'yes'
), (
NULL , '0', 'about_content', 'Y', '1', '<P>请在<A href=./?optionadmin>站点设置</A>[关于内容]中编辑</P>', '20', '8', '关于内容', '1', 'yes'
), (
NULL , '0', 'service_content', 'Y', '1', '<P>请在<A href=./?optionadmin>站点设置</A>[服务内容]中编辑</P>', '20', '8', '服务内容', '1', 'yes'
), (
NULL , '0', 'charset', 'Y', '1', '$charset', '20', '8', '字符集', '1', 'yes'
), (
NULL , '0', 'siteurl', 'Y', '1', '$guessurl', '20', '8', '首页地址（最后没有斜杠）', '1', 'yes'
);"));
	
	$trdb->query(trans_sql_utf8($charset,"INSERT INTO `".$prefix."users` ( `ID` , `user_login` , `user_pass` , `user_email` , `user_fullname` , `user_nickname` , `user_s_email` , `user_msn` , `user_blog` , `user_ip` , `user_registered` , `user_level` , `user_idmode` , `user_activation_key` , `user_status` , `user_description` , `user_addon1` , `user_addon2` , `user_addon3` ) 
VALUES (
NULL , '$user_name', '$user_pass', '$user_email', '', '', '', '', '', '', '0000-00-00 00:00:00', '10', '', '', '0', '', '', '', ''
);"));

}
endif;

function tr_check_mysql_version() {
	global $tr_version;

	// Make sure the server has MySQL 4.0
	$mysql_version = preg_replace('|[^0-9\.]|', '', @mysql_get_server_info());
	if ( version_compare($mysql_version, '4.0.0', '<') )
		die(sprintf(__('<strong>ERROR</strong>: WordPress %s requires MySQL 4.0.0 or higher'), $tr_version));
}

?>
